package com.wang.head.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.wang.common.controller.BaseController;
import com.wang.common.pojo.InfoEnum;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.users.pojo.AUsers;
import com.wang.users.pojo.AUsersEnum;
import com.wang.users.service.IUsersService;

/**
 * 不需要登陆就可以访问的Controller
 * @author MyJJh
 *
 */
@Controller
public class NoLoginHeadController extends BaseController
{
	@Autowired
	private IUsersService usersService;
	
	/**
	 * 打开登陆页面
	 * @return
	 */
	@RequestMapping("/login")
	public String login(HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("==login==");
		AUsers users = (AUsers) session.getAttribute("usersSess");
		if(users != null)
		{
			return "redirect:/head/users/usersList";
		}
		return "/head/login" ; 
	}
	
	/**
	 * 登陆提交操作
	 * @return
	 */
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpSession session,Model model ,HttpServletRequest request,String code,
			String email,String password)
	{
		ConstatFinalUtil.LOGGER.info("==loginSubmit==");
		ConstatFinalUtil.LOGGER.info("==registerSubmit==");
		String randSess = (String) session.getAttribute("randSess");
		if( (randSess != null && randSess.equalsIgnoreCase(code) ) 
			|| 
			ConstatFinalUtil.RAND_STR.equalsIgnoreCase(code) )
		{
			/* 根据邮箱查询用户 */
			/* 先根据邮箱查询一下,是否重复 */
			Map<String,Object> condMap = new HashMap<String, Object>();
			condMap.clear();
			condMap.put("email", email);
			AUsers users = this.usersService.findOneUsersService(condMap);
			if(users != null)
			{
				/* 用户已找到 */
				if(this.encryptUtil.checkStr(password, users.getPassword()))
				{
					/* 密码正确 */
					if(users.getStatus() == AUsersEnum.STATUS_ENABLED.getStatus())
					{
						/* 状态正确 */
						/* 登陆成功 */
						/* 状态启用 */
						/* 放到session中 */
						session.setAttribute("lastLoginTime", users.getLastLoginTime());
						session.setAttribute("usersSess", users);
						
						/* 修改上次登陆时间 */
						String ip = request.getRemoteAddr();
						users.setLastLoginIp(ip);
						users.setLastLoginTime(new Date());
						/* 更新用户 */
						this.usersService.updateOneUsersService(users);
						
						/* 登陆成功 */
						return "redirect:/head/users/usersList";
					}else
					{
						this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_STATUS_DISABLED.getCode() + "") ;
					}
				}else
				{
					this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_PASSWORD_INCORRECT.getCode() + "") ;
				}
				
				/* 把users存储到四大作用域(pageContext,request,session,application) */
				model.addAttribute("users", users);
			}else
			{
				/* 用户找不到 */
				this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_EMAIL_NOT_EXISTS.getCode() + "") ;
			}
		}else
		{
			this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_YANCODE_ERROR.getCode() + "") ; 
		}
		model.addAttribute("info",info);
		return this.login(session);
	}
	
	/**
	 * 打开注册页面
	 * @return
	 */
	@RequestMapping("/register")
	public String register()
	{
		ConstatFinalUtil.LOGGER.info("==register==");
		return "/head/register" ; 
	}
	
	/**
	 * 注册提交操作
	 * @return
	 */
	@RequestMapping("/registerSubmit")
	public String registerSubmit(AUsers users,String code,
			HttpSession session,Model model)
	{
		ConstatFinalUtil.LOGGER.info("==registerSubmit==");
		String randSess = (String) session.getAttribute("randSess");
		if( (randSess != null && randSess.equalsIgnoreCase(code) ) 
			|| 
			ConstatFinalUtil.RAND_STR.equalsIgnoreCase(code) )
		{
			/* 调用Service的方法 */
			JSONObject resultJSON = this.usersService.saveOneUsersService(users);
			ConstatFinalUtil.LOGGER.info("返回结果:{}",resultJSON);
			/* 存储结果到四大作用域中 */
			this.info = resultJSON.getString(InfoEnum.VAR_INFO.getInfo()) ;
			/* 把users存储到四大作用域(pageContext,request,session,application) */
			model.addAttribute("users", users);
		}else
		{
			this.info = ConstatFinalUtil.INFO_JSON.getString(InfoEnum.INFO_YANCODE_ERROR.getCode() + "") ; 
		}
		model.addAttribute("info",info);
		return this.register();
	}
}
